<template>
	<view class="cu-modal show" v-show="showLoginModal">
		<view class="cu-dialog">
			<view class="cu-bar bg-white justify-end">
				<view class="content">输入验证码</view>
				<view class="action" @tap="hideModal">
					<text class="cuIcon-close text-red"></text>
				</view>
			</view>
			<view class="">
				<view class="grid col-1 margin-bottom text-center">
					<view class="">
						<image mode="aspectFit" style="height: 100rpx"
							:src="showLoginModal?'http://211.82.48.36/img/captcha.jpg':''"></image>
					</view>
					<view class="cu-form-group">
						<input :value="captcha" @input="inputCaptcha" placeholder="请输入验证码" />
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white justify-end">
				<view class="action">
					<button class="cu-btn line-black text-green" @tap="hideModal">取消</button>
					<button class="cu-btn bg-black margin-left" @tap="confirmModal">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const app = getApp();
	import md5 from '../../utils/md5.js'
	export default {
		data() {
			return {
				captcha: ''
			};
		},
		/**
		 * 组件的一些选项
		 */
		/**
		 * 组件的对外属性
		 */
		props: {
			showLoginModal: {
				type: Boolean,
				default: false
			}
		},
		/**
		 * 组件的方法列表
		 */
		methods: {
			hideModal() {
				this.$emit("update:showLoginModal", false)
			},

			async confirmModal() {
				uni.showLoading()
				let that = this
				var [err, res] = await uni.request({
					url: 'http://211.82.48.36/j_spring_security_check?j_username=' + uni.getStorageSync(
							'userID') +
						'&j_password=' + md5.hex_md5(uni.getStorageSync('passWord')) + '&j_captcha=' + that
						.captcha,
					method: 'POST',
					header: {
						"Location": "http://211.82.48.36/"
					},
				})
				console.log(res)
				this.setData({
					captcha: ''
				});
				uni.hideLoading();
				if (err) {
					that.msgalert('网络错误');
				}
				if (res.data.indexOf("URP综合教务系统首页") == -1) {
					uni.navigateTo({
						url: '/pages/login/index'
					});
				} else {
					this.$emit("LoginModalConfirm", true);
				}
				
				this.$emit("update:showLoginModal", false)

			},

			inputCaptcha(e) {
				this.setData({
					captcha: e.detail.value
				});
			},
		}
	};
</script>
